package cn.senjoeson.triper.activity;

import android.app.Dialog;
import android.os.Build;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import cn.senjoeson.mylibrary.utilschanged.LogUtils;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.base.BaseActivity;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.DayBean;
import cn.senjoeson.triper.bean.PlaceBean;
import cn.senjoeson.triper.dao.DayBeanDao;
import cn.senjoeson.triper.dao.PlaceBeanDao;

public class SelectTimeActivity extends BaseActivity {


    @BindView(R.id.left_arrow)
    ImageView mLeftArrow;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbarlayout)
    LinearLayout mToolbarlayout;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.DepartTime)
    TextView mDepartTime;
    @BindView(R.id.setdepartTime)
    LinearLayout mSetdepartTime;
    @BindView(R.id.ReturnTime)
    TextView mReturnTime;
    @BindView(R.id.setreturnTime)
    LinearLayout mSetreturnTime;
    @BindView(R.id.breakfasttime)
    TextView mBreakfasttime;
    @BindView(R.id.setbreakfasttime)
    LinearLayout mSetbreakfasttime;
    @BindView(R.id.lunchtime)
    TextView mLunchtime;
    @BindView(R.id.setlunchtime)
    LinearLayout mSetlunchtime;
    @BindView(R.id.dinnertime)
    TextView mDinnertime;
    @BindView(R.id.setdinnertime)
    LinearLayout mSetdinnertime;
    public DayBeanDao mDayBeanDao;
    public DayBean mDayBean;
    public long mDayId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_time;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

        mLeftArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                mDayBean.setDepartTime(mDepartTime.getText().toString());
                mDayBean.setReturnTime(mReturnTime.getText().toString());
                mDayBean.setBreakfastTime(mBreakfasttime.getText().toString());
                mDayBean.setLunchTime(mLunchtime.getText().toString());
                mDayBean.setDinnerTime(mDinnertime.getText().toString());

                DayBean load = mDayBeanDao.load(mDayBean.getDayid());
                if (load == null) {
                    mDayBeanDao.insert(mDayBean);
                } else {
                    mDayBeanDao.update(mDayBean);
                }

                addInitTime(mDayBean);  //把设置的时间添加进去
                onBackPressed();
            }
        });
    }

    @Override
    protected void initView() {
        long planId = getIntent().getLongExtra("planId", -1);
        mDayId = getIntent().getLongExtra("dayId", -1);
        mDayBeanDao = GreenDaoManager.getInstance().getNewSession().getDayBeanDao();
        mDayBean = new DayBean();
        mDayBean.setPlanid(planId);
        mDayBean.setDayid(mDayId);

        List<DayBean> list = mDayBeanDao.queryBuilder().where(DayBeanDao.Properties.Dayid.eq(mDayId)).build().list();
        if(list!=null&&list.size()>0){
            DayBean dayBean = list.get(0);
            mDepartTime.setText(dayBean.getDepartTime());
            mReturnTime.setText(dayBean.getReturnTime());
            mBreakfasttime.setText(dayBean.getBreakfastTime());
            mLunchtime.setText(dayBean.getLunchTime());
            mDinnertime.setText(dayBean.getDinnerTime());
        }


    }

    /**
     * 添加设置的时间
     *
     * @return
     */
    private void addInitTime(DayBean dayBean) {

        PlaceBeanDao placeBeanDao = GreenDaoManager.getInstance().getNewSession().getPlaceBeanDao();

        Random random = new Random();
        //出发时间
        if (!TextUtils.isEmpty(dayBean.getDepartTime())) {
            PlaceBean depart = new PlaceBean();
            depart.setTime(dayBean.getDepartTime());
            depart.setDayid(dayBean.getDayid());
            depart.setPlaceName("现在是出发时间了");
            depart.setId(random.nextLong());
            placeBeanDao.insert(depart);
        }

        //回来时间
        if (!TextUtils.isEmpty(dayBean.getReturnTime())) {
            PlaceBean returnTime = new PlaceBean();
            returnTime.setTime(dayBean.getReturnTime());
            returnTime.setDayid(dayBean.getDayid());
            returnTime.setPlaceName("该回去了哦");
            returnTime.setId(random.nextLong());
            placeBeanDao.insert(returnTime);
        }
        //早餐
        if (!TextUtils.isEmpty(dayBean.getBreakfastTime())) {
            PlaceBean breakfast = new PlaceBean();
            breakfast.setTime(dayBean.getBreakfastTime());
            breakfast.setDayid(dayBean.getDayid());
            breakfast.setPlaceName("早餐时间");
            breakfast.setId(random.nextLong());
            placeBeanDao.insert(breakfast);
        }
        //中餐
        if (!TextUtils.isEmpty(dayBean.getLunchTime())) {
            PlaceBean lunch = new PlaceBean();
            lunch.setTime(dayBean.getLunchTime());
            lunch.setDayid(dayBean.getDayid());
            lunch.setPlaceName("现在是吃午饭的时间了");
            lunch.setId(random.nextLong());
            placeBeanDao.insert(lunch);
        }
        //中餐
        if (!TextUtils.isEmpty(dayBean.getDinnerTime())) {
            PlaceBean dinner = new PlaceBean();
            dinner.setTime(dayBean.getDinnerTime());
            dinner.setDayid(dayBean.getDayid());
            dinner.setPlaceName("累了一天,好好吃顿晚饭吧");
            dinner.setId(random.nextLong());
            placeBeanDao.insert(dinner);
        }

    }


    @OnClick({R.id.setdepartTime, R.id.setreturnTime, R.id.setbreakfasttime, R.id.setlunchtime, R.id.setdinnertime})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.setdepartTime:
                showSetTimeDialog(mDepartTime);
                break;
            case R.id.setreturnTime:
                showSetTimeDialog(mReturnTime);
                break;
            case R.id.setbreakfasttime:
                showSetTimeDialog(mBreakfasttime);
                break;
            case R.id.setlunchtime:
                showSetTimeDialog(mLunchtime);
                break;
            case R.id.setdinnertime:
                showSetTimeDialog(mDinnertime);
                break;
        }

    }

    public void showSetTimeDialog(final TextView mTv) {

        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.select_time_dialog);
        dialog.setTitle("请选择时间");
        final TimePicker timePicker = dialog.findViewById(R.id.timePicker);
        timePicker.setIs24HourView(true);
        TextView mCancel = dialog.findViewById(R.id.cancel);
        TextView mConfirm = dialog.findViewById(R.id.comfirm);

        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    LogUtils.d("小时数：" + timePicker.getHour() + "分钟数:" + timePicker.getMinute());
                    // LogUtils.d(timePicker.get);
                    mTv.setText(transTime(timePicker.getHour()) + ":" + transTime(timePicker.getMinute()));
                }
                dialog.dismiss();


            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                LogUtils.d("时间改变回调");
            }
        });
    }

    public String transTime(int time) {
        if (time < 10) {
            return "0" + time;
        }
        return time + "";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
